var _UrlPrefix = "http://www.fuzhuangchangongxiao.cn/";
const _AppAccess = web_access;

(function ($) {
    // 登录/注册
    var login = {
        init: function () {
            var vm = new Vue({
                el: "#loginBox",

                // 数据
                data: {
                    activeType: true,   // 切换登录和注册
                    phone: null,
                    password: null,
                    code: null,
                    codeType: true,     // 获取验证码
                    codeText: '获取验证码', 
                    codeBtnClass: 'code-not-exist',
                    isCheck: true,     // 校验账号密码是否一致
                },

                // 周期
                mounted: function () {

                },

                watch: {
                    phone: function (newValue, oldValue) {
                        this._checkUserPhone(oldValue, newValue)
                    }
                },

                computed: {
                    codeTextFun: function () {
                        return this.codeText;
                    }
                },

                // 方法
                methods: {
                    onLogin () {
                        var self = this
                        // 用户登录
                        // 前端先校验一下
                        if (this._checkUserInfo()) {
                            // 前端校验成功
                            console.log("前端校验成功")
                            $.ajax({
                                type:"POST",
                                url: _UrlPrefix + "api/web/user/login",
                                data: {
                                    phone: self.phone,
                                    pwd: self.password,
                                    app_access: _AppAccess
                                },
                                success:function(res){
                                    if(200 == res.code || 0 == res.code) {
                                        alert("登录成功");
                                        console.log("res: ", res)
                                        localStorage.setItem("token", res.data.token);
                                        localStorage.setItem("uId", res.data.uid);
                                        if(1 == res.data.type) {
                                            window.location.href = _UrlPrefix + "/pc/public/StaffManage/?type=" + res.data.type + "&uid=" + res.data.uid;
                                        }else if(2 == res.data.type) {
                                            window.location.href = _UrlPrefix + "/pc/public/company/?type=" + res.data.type + "&uid=" + res.data.uid;
                                        }else if(3 == res.data.type) {
                                            window.location.href = _UrlPrefix + "/pc/public/factory/?type=" + res.data.type + "&uid=" + res.data.uid;
                                        }else if(4 == res.data.type) {
                                            window.location.href = _UrlPrefix + "/pc/public/enterprise/?type=" + res.data.type + "&uid=" + res.data.uid;
                                        }else {
                                            console.log("无效用户")
                                        }
                                    }else if(res.code == 30003) {
                                        self.isCheck = false
                                        setTimeout(function () {
                                            self.isCheck = true
                                        }, 1500)
                                    }
                                },
                                error: function(xhr,status,error) {
                                    // console.log("xhr:", xhr)
                                    // console.log("status:", status)
                                    // console.log("error:", error)
                                },
                                complete: function () {

                                }
                            })
                        } else {
                            // 前端校验失败
                            console.log("前端校验失败")
                            self.isCheck = false
                            setTimeout(function () {
                                self.isCheck = true
                            }, 1500)
                        }
                    },
                    onRegistered () {
                        // 用户注册
                        var self = this
                        if (this._checkUserInfo()) {
                            $.ajax({
                                type:"POST",
                                url: _UrlPrefix + "api/web/user/add",
                                data: {
                                    phone: self.phone,
                                    pwd: self.password,
                                    code: self.code,
                                    app_access: _AppAccess
                                },
                                success:function(res){
                                    console.log("注册: ", res)
                                    if(200 == res.code || 0 == res.code) {
                                        alert("注册成功")
                                        console.log("res: ", res)
                                    }else if(res.code == 30002) {
                                        alert("手机号已注册")
                                    }else if(res.code == 30003) {
                                        self.isCheck = false
                                        setTimeout(function () {
                                            self.isCheck = true
                                        }, 1500)
                                    }
                                },
                                error: function(xhr,status,error) {
                                    // console.log("xhr:", xhr)
                                    // console.log("status:", status)
                                    // console.log("error:", error)
                                },
                                complete: function () {

                                }
                            })
                        } else {
                            // 前端校验失败
                            console.log("前端校验失败")
                            self.isCheck = false
                            setTimeout(function () {
                                self.isCheck = true
                            }, 1500)
                        }
                    },
                    onSwitchLogin () {
                        // 切换到登录状态
                        this.activeType = true
                        this.phone = null
                        this.password = null
                        this.code = null
                    },
                    onSwitchRegister () {
                        // 切换到注册状态
                        this.activeType = false
                        this.phone = null
                        this.password = null
                        this.code = null
                        this.codeType = true
                        this.codeText = '获取验证码'
                        this.codeBtnClass = 'code-not-exist'
                        // clearInterval(this.t1)
                    },
                    onGetCode () {
                        // 获取验证码
                        var self = this;
                        if(!this.phone || this.phone.length != 11) {
                            return alert("请输入正确的手机号码");
                        }
                        $.ajax({
                            type:"POST",
                            url: _UrlPrefix + "api/member/yzm",
                            data: {
                                phone: self.phone,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 0) {
                                    if (self.codeType) {
                                        self.codeType = false
                                        self.codeBtnClass = 'code-exist'
                                        self._second = 60
                                        self.codeText = self._second + "s后重置"
                                        // 设置倒计时
                                        self.t1 = setInterval(function () {
                                            if (self._second > 0) {
                                                self._second--;
                                                self.codeText = self._second + "s后重置"
                                            } else {
                                                self.codeType = true;
                                                self.codeBtnClass = 'code-not-exist'
                                                self.codeText = "获取验证码"
                                                clearInterval(self.t1)
                                            }
                                        }, 1000)
                                    }
                                }
                            },
                            error: function(xhr,status,error) {
                                // console.log("xhr:", xhr)
                                // console.log("status:", status)
                                // console.log("error:", error)
                            },
                            complete: function () {

                            }
                        })
                        
                    },
                    _checkUserInfo () {
                        if(this.phone && this.phone.length == 11 && this.password && this.password.length > 0) {
                            return true;
                        }
                        return false;
                    },
                    _checkUserPhone (oldValue, newValue) {
                        if(!newValue || newValue == null) return
                        if(newValue.length > 11) {
                            this.phone = oldValue
                            return
                        }
                        for(var i = 0;i < newValue.length;i++) {
                            if(newValue[i] < '0' || newValue[i] > '9') {
                                this.phone = oldValue
                            }
                        }                 
                    }
                },

                // 周期
                created: function () {
                    // 页面初始化接口请求
                },
            })
        }
    }
    login.init()
})(jQuery)